package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.dao.SetmealDao;
import com.itheima.dao.SetmealDishDao;
import com.itheima.dto.SetmealDto;
import com.itheima.exception.CustomException;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishDao setmealDishDao;

    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        //1.判断套餐名称是否存在
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName, setmealDto.getName());
        int count = this.count(wrapper);
        if (count > 0) {
            throw new CustomException("套餐名称【" + setmealDto.getName() + "】已经存在！");
        }

        //2.保存套餐基本信息到套餐表setmeal中
        this.save(setmealDto);

        //3.保存套餐包含的菜品信息到套餐菜品表setmeal_dish中
        // 3.1:获取套餐id
        Long setmealId = setmealDto.getId();

        // 3.2：设置每个菜品所属的套餐id 保存到setmeal_dish表中
        setmealDto.getSetmealDishes().forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
            setmealDishDao.insert(setmealDish);
        });

    }

    @Override
    public void deleteWithDishes(Long[] ids) {
        //0.判断是否有传递要删除的套餐id
        if (ids == null || ids.length == 0) {
            throw new CustomException("要删除的套餐id不能为空！");
        }

        //1.判断要删除的套餐是否处于停售状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);         //where id in (1,2)
        wrapper.eq(Setmeal::getStatus, 1);   //where status = 1  启售的
        List<Setmeal> list = this.list(wrapper);
        if (list != null && list.size() > 0) {
            for (Setmeal setmeal : list) {
                throw new CustomException("套餐【" + setmeal.getName() + "】处于启售状态！");
            }
        }

        //2.根据id去套餐表setmeal中删除套餐    Arrays.asList(ids)：将数组转为list集合
        this.removeByIds(Arrays.asList(ids));

        //3.根据id去套餐菜品表setmeal_dish中删除当前套餐下包含的菜品信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId, ids); //where setmealId in (1,2)
        setmealDishDao.delete(setmealDishLambdaQueryWrapper);
    }

    @Override
    public SetmealDto getSetmealById(Long id) {
        //根据ID餐查询基本信息
        Setmeal setmeal = this.getById(id);

        //根据ID查询菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishes = setmealDishDao.selectList(wrapper);

        //3.将根据套餐id查询套餐基本信息和菜品信息封装到SetmealDto对象中并返回
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);
        return setmealDto;
    }

    @Override
    public void updateSetmeal(SetmealDto setmealDto) {
        LambdaQueryWrapper<Setmeal> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(setmealDto.getName() != null, Setmeal::getName, setmealDto.getName());
        wrapper1.ne(setmealDto.getId() != null, Setmeal::getId, setmealDto.getId());
        int count = this.count(wrapper1);
        if (count > 0) {
            throw new CustomException("套餐[" + setmealDto.getName() + "]已存在,无需再添加");
        }
        this.updateById(setmealDto);

        Long setmealDtoId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(SetmealDish::getSetmealId, setmealDtoId);
        setmealDishDao.delete(wrapper2);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().forEach(setmealDish -> {
            setmealDish.setId(null);
            setmealDish.setSetmealId(setmealDtoId);
            setmealDishDao.insert(setmealDish);
        });
    }

    @Override
    public List<Setmeal> startAndStop(Long[] ids, String status) {
        if (!"0".equals(status) && !"1".equals(status)) {
            throw new CustomException("未知的售卖状态");
        }
        if (ids == null || ids.length == 0) {
            throw new CustomException("请先勾选套餐！");
        }
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        List<Setmeal> setmeals = this.list(wrapper);
        setmeals = setmeals.stream().map(setmeal -> {
            if ("0".equals(status)) {
                setmeal.setStatus(0);
            } else {
                setmeal.setStatus(1);
            }
            this.updateById(setmeal);
            return setmeal;
        }).collect(Collectors.toList());
        return setmeals;

    }
}
